package com.aliang.shopping.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.aliang.shopping.common.PageResult;
import com.aliang.shopping.common.ResultCodeEnum;
import com.aliang.shopping.config.OssConfig;
import com.aliang.shopping.contact.BasicConstant;
import com.aliang.shopping.contact.SqlColumnConstant;
import com.aliang.shopping.exception.DataException;
import com.aliang.shopping.exception.ServiceException;
import com.aliang.shopping.mapper.BusinessMapper;
import com.aliang.shopping.model.converter.BusinessConverter;
import com.aliang.shopping.model.converter.CommonConverter;
import com.aliang.shopping.model.dto.business.BusinessSearchDTO;
import com.aliang.shopping.model.dto.login.EmailLoginDTO;
import com.aliang.shopping.model.dto.login.LoginDTO;
import com.aliang.shopping.model.dto.login.PhoneLoginDTO;
import com.aliang.shopping.model.enums.UserTypeEnum;
import com.aliang.shopping.model.po.Business;
import com.aliang.shopping.model.vo.CommonUserInfoVO;
import com.aliang.shopping.model.vo.business.BusinessExportVO;
import com.aliang.shopping.model.vo.business.BusinessVO;
import com.aliang.shopping.security.auth.Payload;
import com.aliang.shopping.service.BusinessService;
import com.aliang.shopping.util.PasswordUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ShiBaLover
 * @description 针对表【business(商家表)】的数据库操作Service实现
 * @createDate 2024-10-19 14:31:33
 */
@Service
@AllArgsConstructor
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business>
        implements BusinessService {
    private final CommonConverter<BusinessVO> commonConverter = new CommonConverter<>();

    private final BusinessConverter businessConverter;
    private final OssConfig ossConfig;

    @Override
    public PageResult<BusinessVO> selectPage(BusinessSearchDTO pageDTO) {
        //查询数据
        IPage<BusinessVO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        IPage<BusinessVO> mapperPage = this.baseMapper.getPage(page, pageDTO);

        //转换数据
        mapperPage.getRecords().forEach(vo -> {
            // 设置完整头像
            if (StrUtil.isNotBlank(vo.getAvatar()))
                vo.setAvatar(ossConfig.getUrl() + vo.getAvatar());
            //设置完整资质证明
            if (!vo.getQualification().isEmpty()) {
                vo.setQualification(StrUtil.replace(vo.getQualification(), BasicConstant.REPLACE_IMAGE, ossConfig.getUrl()));
            }
        });
        return commonConverter.toPageResult(mapperPage);
    }

    @Override
    public List<BusinessExportVO> getExportList(BusinessSearchDTO pageDTO) {
        List<BusinessVO> list = this.baseMapper.getVoList(pageDTO);
        if (list.isEmpty()) {
            throw new DataException(ResultCodeEnum.NOT_QUERY_RESULT);
        }
        //转换数据
        list.forEach(vo -> {
            if (ObjUtil.isNotNull(vo.getAvatar()))
                vo.setAvatar(ossConfig.getUrl() + vo.getAvatar());
        });
        return businessConverter.toImportDTOList(list);
    }

    /**
     * 登录方法
     *
     * @param loginDTO
     * @return
     */
    @Override
    public Payload login(LoginDTO loginDTO) {
        //根据用户名查询用户信息
        Business business = this.baseMapper.selectOne(new QueryWrapper<Business>().eq(SqlColumnConstant.USERNAME, loginDTO.getUsername())
                .eq(SqlColumnConstant.STATUS, SqlColumnConstant.STATUS_OK));
        //判断是否为空
        if (ObjUtil.isEmpty(business)) {
            throw new DataException(ResultCodeEnum.USERNAME_IS_NOT_EXIT);
        }
        // 判断密码是否一致
        if (PasswordUtil.isNotEqual(business.getPassword(), loginDTO.getPassword())) {
            throw new DataException(ResultCodeEnum.PASSWORD_NOT_MATCH);
        }
        return new Payload(business.getId(), UserTypeEnum.BUSINESS.getCode());
    }


    @Override
    public Payload emailLogin(EmailLoginDTO loginDTO) {
        Business business = this.baseMapper.selectOne(new QueryWrapper<Business>().eq(SqlColumnConstant.EMAIL, loginDTO.getEmail())
                .eq(SqlColumnConstant.STATUS, SqlColumnConstant.STATUS_OK));
        if (!ObjUtil.isEmpty(business)) {
            //返回载荷
            return new Payload(business.getId(), UserTypeEnum.BUSINESS.getCode());
        }
        return null;
    }

    @Override
    public Payload phoneLogin(PhoneLoginDTO loginDTO) {
        Business business = this.baseMapper.selectOne(new QueryWrapper<Business>().eq(SqlColumnConstant.PHONE, loginDTO.getPhone())
                .eq(SqlColumnConstant.STATUS, SqlColumnConstant.STATUS_OK));
        if (!ObjUtil.isEmpty(business)) {
            //返回载荷
            return new Payload(business.getId(), UserTypeEnum.BUSINESS.getCode());
        }
        return null;
    }

    @Override
    public CommonUserInfoVO getUserInfo(Long id) {
        Business business = this.baseMapper.selectById(id);
        if (ObjUtil.isEmpty(business)) {
            throw new ServiceException(ResultCodeEnum.USER_NOT_LOGIN);
        }
        return this.businessConverter.toUserInfoVO(business);
    }
}




